<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="query"
        inline size="mini"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent
      >
        <el-form-item label="IMEI:">
          <el-input
              clearable
              v-model="query.imei"
              placeholder="请输入设备IMEI"
          />
        </el-form-item>
        <el-form-item label="RFID:">
          <el-input
              clearable
              v-model="query.rfid"
              placeholder="请输入设备RFID"
          />
        </el-form-item>
        <el-form-item label="型号:">
          <el-select v-model="query.model_id" style="width: 100%">
            <el-option
                :key="i"
                :value="item.id"
                :label="item.name"
                v-for="(item, i) in modelList"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="服务IP:">
          <el-input
              clearable
              v-model="query.ip"
              placeholder="请输入设备服务IP"
          />
        </el-form-item>
        <el-form-item label="版本号:">
          <el-input
              clearable
              v-model="query.ver"
              placeholder="请输入设备版本号"
          />
        </el-form-item>
        <el-form-item label="蓝牙MAC:">
          <el-input
              clearable
              v-model="query.mac"
              placeholder="请输入设备蓝牙MAC"
          />
        </el-form-item>
        <el-form-item>
          <el-button
              type="primary"
              icon="el-icon-search"
              class="ele-btn-icon"
              @click="reload"
          >查询
          </el-button>
          <el-button @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
      <div class="toolbar-box" style="margin-bottom: 4px">
        <div class="tool-btn">
          <el-button
              size="mini"
              type="default"
              icon="el-icon-upload"
              @click="importTable"
          >批量导入</el-button
          >
          <el-button
              size="mini"
              type="default"
              icon="el-icon-download"
              @click="exportTable"
          >导出</el-button
          >
        </div>
        <div class="tool-tab">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-plus"
            @click="handleAdd"
            >新增</el-button
          >
        </div>
      </div>
      <!-- 数据表格 -->
      <el-table
        border
        lazy
        ref="table"
        :data="data"
        v-loading="loading"
        height="calc(100vh - 410px)"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center">
        </el-table-column>
        <el-table-column label="序号" width="55" align="center" type="index">
          <template slot-scope="scope">
            <span
              v-text="$setTableIndex(query.page, query.limit, scope.$index)"
            >
            </span>
          </template>
        </el-table-column>
        <el-table-column
          label="IMEI"
          align="center"
          prop="imei"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="RFID"
          align="center"
          prop="rfid"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column label="服务IP" align="center" prop="ip">
        </el-table-column>
        <el-table-column label="型号" align="center" prop="model_name">
        </el-table-column>
        <el-table-column label="蓝牙MAC" align="center" prop="mac">
        </el-table-column>
        <el-table-column label="版本号" align="center" prop="ver">
        </el-table-column>
        <el-table-column
          label="创建时间"
          align="center"
          prop="create_time"
          width="160"
        >
        </el-table-column>
        <el-table-column label="操作" align="center" :resizable="false">
          <template slot-scope="{ row }">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              :underline="false"
              @click="handleEdit(row)"
              >修改</el-button
            >
            <el-popconfirm
              title="确定要删除该型号吗？"
              @confirm="remove(row)"
              class="ele-action"
            >
              <el-button
                size="mini"
                type="danger"
                slot="reference"
                icon="el-icon-delete"
                :underline="false"
                >删除</el-button
              >
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        :total="total"
        :page.sync="query.page"
        :limit.sync="query.limit"
        @pagination="getData"
      />
    </el-card>
    <el-dialog
      :title="title"
      width="1000px"
      append-to-body
      @closed="cancel"
      :visible.sync="showEdit"
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="IMEI" prop="imei">
              <el-input v-model="form.imei" placeholder="请输入设备IMEI" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="服务IP" prop="ip">
              <el-input v-model="form.ip" placeholder="请输入设备服务IP" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="RFID" prop="rfid">
              <el-input v-model="form.rfid" placeholder="请输入设备RFID" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="版本号" prop="ver">
              <el-input v-model="form.ver" placeholder="请输入设备版本号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备型号" prop="model_id">
              <el-select v-model="form.model_id" style="width: 100%">
                <el-option
                  :key="i"
                  :value="item.id"
                  :label="item.name"
                  v-for="(item, i) in modelList"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="蓝牙MAC" prop="mac">
              <el-input v-model="form.mac" placeholder="请输入设备蓝牙MAC" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="批量导入"
      width="500px"
      append-to-body
      @closed="cancelImport"
      :visible.sync="showImport"
      :close-on-click-modal="false"
    >
      <dl class="uploadlist">
        <dt>上传文件：</dt>
        <dd>
          <el-upload
            :multiple="false"
            action=""
            :on-change="handleFileChange"
            :on-remove="handleFileRemove"
            :auto-upload="false"
            :file-list="fileList"
            :limit="1"
            :on-exceed="handleExceed"
            :show-file-list="true"
            class="upload-file-uploader"
            ref="fileUpload"
          >
            <!-- 上传按钮 -->
            <el-button type="primary">选择文件</el-button>
          </el-upload>
        </dd>
      </dl>
      <dl class="uploadlist">
        <dt>模板：</dt>
        <dd>
          <a href="https://api.gdjx-ai.com/uploads/imei.xlsx" target="_blank"
            >下载模板</a
          >
        </dd>
      </dl>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitImportFile">确 定</el-button>
        <el-button @click="cancelImport">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";

export default {
  name: "DeviceIMEI",
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      loading: false, // 表格加载状态
      ids: [], // 选中数组
      selection: [], // 表格选中数据
      total: 0, // 数据总数
      data: [], // 表格数据接口
      modelList: [],
      // 表格搜索条件
      query: {
        page: 1,
        limit: 10,
      },
      model_query: {
        page: 1,
        limit: 99999,
      },
      showEdit: false, // 是否显示编辑弹窗
      showImport: false, // 显示导入弹窗
      fileList: [], // 上传发票文件列表
      title: "",
      form: {}, //  新增编辑表单// 表单校验
      rules: {
        modelName: [
          { required: true, message: "型号名称不能为空", trigger: "blur" },
        ],
        imei: [{ required: true, message: "IMEI不能为空", trigger: "blur" }],
        // ip: [{ required: true, message: "服务IP不能为空", trigger: "blur" }],
        // rfid: [{ required: true, message: "RFID不能为空", trigger: "blur" }],
        // ver: [{ required: true, message: "版本号不能为空", trigger: "blur" }],
        // model_id: [
        //   { required: true, message: "设备型号不能为空", trigger: "blur" },
        // ],
        // mac: [{ required: true, message: "蓝牙MAC不能为空", trigger: "blur" }],
      },
    };
  },
  mounted() {
    this.getData();
    this.getModelList();
  },
  methods: {
    /* 获取表格数据 */
    getData() {
      this.loading = true;
      this.$http
        .get("/device_imei/index", { params: this.query })
        .then((res) => {
          this.loading = false;
          console.log(res);
          this.total = res.data.count;
          this.data = res.data.data;
        })
        .catch((e) => {
          this.loading = false;
          this.$message.error(e.message);
        });
    },
    getModelList() {
      this.$http
        .get("/device_model/index", { params: this.model_query })
        .then((res) => {
          this.modelList = res.data.data;
        })
        .catch((e) => {
          this.loading = false;
          this.$message.error(e.message);
        });
    },
    /* 查询数据 */
    reload() {
      this.getData();
    },
    /* 重置搜索 */
    reset() {
      this.query = {
        page: 1,
        limit: 10,
      };
      this.reload();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.selection = selection;
    },
    /* 删除 */
    remove(row) {
      this.loading = true;
      const params = { id: row.id };
      this.$http
        .get("/device_imei/delete", { params: params })
        .then((res) => {
          this.loading = false;
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.getData();
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch((e) => {
          this.loading = false;
          this.$message.error(e.message);
        });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error("请至少选择一条数据");
        return;
      }
      this.$confirm("确定要删除选中的型号吗?", "提示", {
        type: "warning",
      })
        .then(() => {
          this.loading = true;
          this.$http
            .post("", { id: this.ids })
            .then((res) => {
              this.loading = false;
              if (res.data.code === 0) {
                this.$message.success(res.data.msg);
                this.getData();
              } else {
                this.$message.error(res.data.msg);
              }
            })
            .catch((e) => {
              this.loading = false;
              this.$message.error(e.message);
            });
        })
        .catch(() => {});
    },
    /* 新增数据 */
    handleAdd() {
      this.showEdit = true;
      this.title = "新增设备IMEI";
      this.form = {};
    },
    /* 修改数据 */
    handleEdit(row) {
      const id = row.id;
      this.showEdit = true;
      this.title = "修改设备IMEI";
      this.form = {};
      this.$http
        .get("/device_imei/info?id=" + id)
        .then((res) => {
          console.log(res);
          this.form = res.data.data;
        })
        .catch((e) => {
          this.loading = false;
          this.$message.error(e.message);
        });
    },
    /* 取消新增修改 */
    cancel() {
      this.showEdit = false;
      this.form = {};
    },
    /* 提交表单 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.$http
            .post("/device_imei/edit", this.form)
            .then((res) => {
              if (res.data.code == "-1") {
                this.$message.error(res.data.msg);
              } else {
                this.$message.success("操作成功");
                this.cancel();
                this.getData();
              }
            })
            .catch((e) => {
              this.loading = false;
              this.$message.error(e.message);
            });
        }
      });
    },
    /* 导入数据 */
    importTable() {
      this.showImport = true;
    },
    /* 取消导入 */
    cancelImport() {
      this.showImport = false;
    },
    // 上传组件上传的文件变更
    handleFileChange(file, fileList) {
      this.fileList = [];
      this.fileList = fileList;
    },
    // 删除上传的文件
    handleFileRemove(file, fileList) {
      this.fileList = [];
      this.fileList = fileList;
    },
    // 文件个数超出
    handleExceed() {
      this.$modal.msgError(`只能上传1个文件!`);
    },
    /* 提交导入的文件 */
    submitImportFile() {
      if (this.fileList.length > 0) {
        // this.$modal.loading("正在上传文件，请稍候...");
        let form = new FormData();
        form.append("file", this.fileList[0].raw);
        this.$http
          .post("/device_imei/importExcel", form)
          .then((res) => {
            console.log(res);
            if (res.data.success) {
              this.$message.success("导入成功！");
              this.showImport = false;
              this.getData();
            } else {
              this.$message.error(res.data.msg);
            }
          })
          .catch((e) => {
            this.loading = false;
            this.$message.error(e.message);
          });
      } else {
        this.$message.error("请选择上传文件");
      }
    },
    /* 导出数据 */
    exportTable() {
      this.$http
        .get("/device_imei/exportExcel", {
          params: { limit: 999999 },
        })
        .then((res) => {
          if (res.data.success) {
            window.open(res.data.data);
          }
        })
        .catch((e) => {
          this.loading = false;
          this.$message.error(e.message);
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.el-dialog:not(.ele-dialog-form)
  .el-dialog__body
  .el-form
  .el-form-item:last-child {
  margin-bottom: 22px;
}
.toolbar-box {
  display: flex;
  align-items: center;
  justify-content: space-between;
  .tool-tab {
    height: 32px;
    ul {
      height: 100%;
      display: flex;
      list-style: none;
      align-items: center;
      li {
        height: 100%;
        padding: 0 10px;
        border-right: 2px solid #ddd;
        &:first-child {
          padding-left: 0;
        }
        &:last-child {
          padding-right: 0;
          border-right: 0;
        }
        span {
          display: block;
          font-size: 14px;
          cursor: pointer;
          padding: 7px 10px;
          border-radius: 4px;
          border: 1px solid #ddd;
          &.active {
            color: #ffffff;
            border-color: #0f40f5;
            background: #0f40f5;
          }
        }
      }
    }
  }
}

.uploadlist {
  margin: 0;
  display: flex;
  margin-bottom: 15px;
  justify-content: flex-start;
  dt {
    height: 40px;
    font-size: 16px;
    line-height: 40px;
  }
  dd {
    font-size: 16px;
    width: calc(100% - 130px);
    margin: 0;
    line-height: 40px;
  }
}
</style>
